import { readonly } from "vue";
import { BasicColumn, FormSchema } from "/@/components/Table"
import DictEnum from "/@/enums/dictEnum";
import { render } from "/@/utils/common/renderUtils";

enum IdpAssociationWay {
  FIELD = 'field',
  BIND = 'bind',
}

enum MatchField {
  PHONE = 'phone',
  EMAIL = 'email',
}

const associationWayFilter = {
  'wechatMpOAuth': [IdpAssociationWay.BIND],
  'gitee': [IdpAssociationWay.BIND, IdpAssociationWay.FIELD],
  'wechatPc': [IdpAssociationWay.BIND],
  'wechatMpQrcode': [IdpAssociationWay.BIND],
  'wechatMaQrcode': [IdpAssociationWay.FIELD],
}

const matchFieldFilter = {
  'gitee': [MatchField.EMAIL],
  'wechatMaQrcode': [MatchField.PHONE],
}

export const beforeFormSchema: FormSchema[] = [
  {
    field: "idpConnectType",
    label: "idpConnectType",
    component: "Input",
    show: false,
  },
  {
    field: "id",
    label: "连接id",
    ifShow: ({ values }) => values.id,
    componentProps: {
      readonly: true
    },
    colProps: { span: 12 },
    component: "Input"
  },
  {
    field: "displayName",
    label: "显示名称",
    required: true,
    colProps: { span: 12 },
    component: "Input"
  }
]

export const afterFormSchema: FormSchema[] = [
  {
    field: "callbackUrl",
    label: "回调地址",
    required: false,
    colProps: { span: 24 },
    helpMessage: '需要将当前地址配置到第三方回调地址上',
    component: "Input",
    ifShow: ({ values }) => values.callbackUrl,
    render: ({ model, field }) => {
      return render.renderSpan(model[field])
    },
  },
  {
    field: "accountBind",
    label: "账号绑定",
    required: true,
    colProps: { span: 24 },
    component: "Switch",
    defaultValue: false,
    componentProps: {
      checkedChildren: "是",
      unCheckedChildren: "否",
    },
  },

  {
    field: "associationWay",
    label: "关联方式",
    required: false,
    ifShow: ({ values }) => values.accountBind,
    colProps: { span: 24 },
    component: "JDictSelectTag",
    defaultValue: null,
    componentProps: ({ formModel, formActionType }) => {
      return {
        type: 'radio',
        dictType: DictEnum.ExtIdpAssociationWay,
        filter: (item) => {
          console.log('associationWay', item, formModel.idpConnectType)
          return associationWayFilter[`${formModel.idpConnectType}`]?.findIndex(v => v == item) >= 0
        },
      }
    }
  },
  {
    field: "matchFields",
    label: "匹配字段",
    required: false,
    ifShow: ({ values }) => values.associationWay == IdpAssociationWay.FIELD,
    colProps: { span: 24 },
    component: "JDictSelectTag",
    defaultValue: [],
    componentProps: ({ formModel, formActionType }) => {
      return {
        type: 'checkbox',
        defaultSelectAll: true,
        valueToString: false,
        dictType: DictEnum.ExtIdpMatchField,
        filter: (item) => {
          console.log('matchFields', item, formModel.idpConnectType)
          return matchFieldFilter[`${formModel.idpConnectType}`]?.findIndex(v => v == item) >= 0
        },
      }
    }
  },

]
